package com.caseeye.service

import com.caseeye.exception.LoginException
import org.springframework.beans.factory.annotation.Required
import javax.annotation.Resource
import com.caseeye.repository.UserRepository
import com.caseeye.model.User
import org.springframework.stereotype.Component
import com.caseeye.base.BaseService

@Component
class LoginService extends BaseService {

    @Required
    @Resource
    var userRepository : UserRepository = null

    def setUserRespository(userRepository : UserRepository) : Unit = this.userRepository = userRepository

    def login(loginId : String, password : String) : Unit = {
        val userInDb = userRepository.findUserByLoginId(loginId)

        userInDb match {
            case Some(u) =>
                if (u.getPassword == password) {
                    u.touchLoginDate()
                    u.save()
                } else throw new LoginException()
            case None => throw new LoginException()
        }
    }
}